/*
 * Copyright (c) 2020. André Mion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andremion.louvre.data;

import com.andremion.louvre.loader.LoaderCallback;

import ohos.aafwk.ability.Ability;
import ohos.data.resultset.CombinedResultSet;
import ohos.data.resultset.ResultSet;
import ohos.data.resultset.TableResultSet;
import ohos.media.photokit.metadata.AVStorage;

import static com.andremion.louvre.data.MediaQuery.BUCKET_PROJECTION;

/**
 * for media and bucket data
 */
public class MediaLoader implements LoaderCallback<ResultSet> {
    static final long ALL_MEDIA_BUCKET_ID = 0;

    private static final int TIME_LOADER = 0;
    private static final int BUCKET_LOADER = 1;
    private static final int MEDIA_LOADER = 2;

    @Override
    public void setCallback(ResultSet resultSet) {
        if (mCallbacks != null) {
            mCallbacks.onMediaLoadFinished(resultSet);
        }
    }

    /**
     * Callbacks
     */
    public interface Callbacks {
        void onBucketLoadFinished(ResultSet data);

        void onMediaLoadFinished(ResultSet data);
    }

    private Ability mActivity;
    private Callbacks mCallbacks;
    private String mTypeFilter;

    /**
     * MediaLoader
     */
    public MediaLoader() {
        mTypeFilter = "1";
    }

    /**
     * onAttach
     *
     * @param activity  Ability
     * @param callbacks CallBack
     */
    public void onAttach(Ability activity, Callbacks callbacks) {
        mActivity = activity;
        mCallbacks = callbacks;
    }

    /**
     * onDetach
     */
    public void onDetach() {
        mActivity = null;
        mCallbacks = null;
    }

    /**
     * setMediaTypes
     *
     * @param mediaTypes mediaTypes
     */
    public void setMediaTypes(String[] mediaTypes) {
        StringBuilder filter = new StringBuilder();
        for (String type : mediaTypes) {
            if (filter.length() > 0) {
                filter.append(",");
            }
            filter.append(String.format("'%s'", type));
        }
        if (filter.length() > 0) {
            mTypeFilter = AVStorage.Images.Media.MIME_TYPE + " IN (" + filter + ")";
        }
    }

    /**
     * loadBuckets
     */
    public void loadBuckets() {
        ensureActivityAttached();
    }

    /**
     * loadByBucket
     *
     * @param bucketId ID
     */
    public void loadByBucket(long bucketId) {
        ensureActivityAttached();
    }

    /**
     * Ensure that a FragmentActivity is attached to this loader.
     */
    private void ensureActivityAttached() {
        if (mActivity == null) {
            throw new IllegalStateException("The FragmentActivity was not attached!");
        }
    }

    /**
     * Add "All Media" item as the first row of bucket items.
     *
     * @param cursor The original data of all bucket items
     * @return The data with "All Media" item added
     */
    private ResultSet addAllMediaBucketItem(ResultSet cursor) {
        if (cursor == null || !cursor.goTo(0)) {
            return null;
        }
        ensureActivityAttached();
        long id = ALL_MEDIA_BUCKET_ID;
        String data = cursor.getString(cursor.getColumnIndexForName(AVStorage.Images.Media.DATA));
        TableResultSet allMediaRow = new TableResultSet(BUCKET_PROJECTION);
        return new CombinedResultSet(new ResultSet[]{allMediaRow, cursor});
    }
}
